<?php

/**
 * This is the model class for table "{{log}}".
 *
 * The followings are the available columns in table '{{log}}':
 * @property integer $zlo_id
 * @property string $zm_id
 * @property string $zlo_name
 * @property integer $zlo_cmp_id
 * @property integer $inputtime
 */
class Log extends CActiveRecord
{
	//开始时间
	public $startime;
	//结束时间
	public $endtime;
	//管理员姓名
	public $zda_name;
	//管理员姓名
	public $cmp_sub;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Log the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{log}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zlo_cmp_id, inputtime', 'numerical', 'integerOnly'=>true),
			array('zm_id,cmp_sub', 'length', 'max'=>50),
			array('zlo_name,startime,endtime,zda_name', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zlo_id, zm_id, zlo_name, zlo_cmp_id, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'fAction'=>array(self::HAS_ONE, 'MemGroup', array('name'=>'zlo_name'),'select'=>'zmg_title'),//前台操作名称
			'mAction'=>array(self::HAS_ONE, 'AdmGroup', array('name'=>'zlo_name'),'select'=>'zag_title'),//后台操作名称
			'adminer'=>array(self::HAS_ONE, 'Adminer', array('zda_id'=>'zm_id')),//后台操作名称
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zlo_id' => 'Zlo',
			'zm_id' => 'Zm',
			'zlo_name' => 'Zlo Name',
			'zlo_cmp_id' => 'Zlo Cmp',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zlo_id',$this->zlo_id);
		$criteria->compare('zlo_name',$this->zlo_name,true);
		$criteria->compare('zlo_cmp_id',$this->zlo_cmp_id);
		
		//定义开始时间查询
		if(!empty($this->startime)){
			$time = strtotime($this->startime);
			$criteria->addCondition(" inputtime>$time ");
		}
		//定义结束时间查询
		if(!empty($this->endtime)){
			$time = strtotime($this->endtime);
			$criteria->addCondition(" inputtime<$time ");
		}
		if(empty($this->startime)&&empty($this->endtime)){
			$criteria->compare('inputtime',$this->inputtime);
		}
		//定义管理员姓名查询
		if(!empty($this->zda_name)){
			$criteria->join = ' LEFT JOIN {{adminer}} adm ON adm.zda_id = t.zm_id ';
			$criteria->addCondition(" adm.zda_name like '%{$this->zda_name}%' ");
		}
		//定义商家子账户名称
		if(!empty($this->cmp_sub)){
			$zm_id = Company::model()->getCmpUserName().":".trim($this->cmp_sub);
			$criteria->addCondition("zm_id like '%{$zm_id}%' ");
		}
		if(empty($this->zda_name)&&empty($this->cmp_sub)){
			$criteria->compare('zm_id',$this->zm_id,true);
		}
		
		$criteria->order = ' inputtime DESC ';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function afterFind(){
		if($this->zlo_cmp_id>0){
			$cmpsubname = explode(':',$this->zm_id);
			$this->cmp_sub = MemSub::model()->findByAttributes(array('zms_username'=>$cmpsubname[1]));
		}
		return true;
	}
	
}